package cn.huaguo.infrastructure.persistent.repository;

import cn.huaguo.domain.campus.model.entity.CampusEntity;
import cn.huaguo.domain.campus.model.valobj.CampusVO;
import cn.huaguo.domain.campus.repository.ICampusRepository;
import cn.huaguo.infrastructure.persistent.dao.ICampusDao;
import cn.huaguo.infrastructure.persistent.po.Campus;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description 校区仓储实现类
 * @Author wangQian
 * @Date 2024/5/15
 */
@Repository
public class CampusRepository implements ICampusRepository {
    @Resource
    private ICampusDao campusDao;

    /**
     * 新增校区
     *
     * @param campusVO
     */
    @Override
    public void addCampus(CampusVO campusVO) {
        campusDao.addCampus(Campus.builder()
                        .campusAddress(campusVO.getCampusAddress())
                        .campusName(campusVO.getCampusName())
                        .createTime(LocalDateTime.now())
                        .updateTime(LocalDateTime.now())
                        .build());
    }

    /**
     * 获取所有校区信息
     *
     * @return
     */
    @Override
    public List<CampusEntity> listAllCampus() {
        List<CampusEntity> campusEntityList = new ArrayList<>();

        List<Campus> campusList = campusDao.listCampus(null);

        for (Campus campus : campusList) {
            campusEntityList.add(CampusEntity.builder()
                            .id(campus.getId())
                            .campusName(campus.getCampusName())
                            .campusAddress(campus.getCampusAddress())
                            .build());
        }

        return campusEntityList;
    }

    /**
     * 根据id获取校区信息
     *
     * @param id
     * @return
     */
    @Override
    public CampusEntity getCampusById(Integer id) {

        Campus campus = campusDao.listCampus(id).get(0);

        return CampusEntity.builder()
                .id(campus.getId())
                .campusName(campus.getCampusName())
                .campusAddress(campus.getCampusAddress())
                .build();
    }

    /**
     * 根据id更新校区信息
     *
     * @param campusEntity
     * @return
     */
    @Override
    public Boolean updateCampus(CampusEntity campusEntity) {
        int result = campusDao.updateCampus(Campus.builder()
                        .id(campusEntity.getId())
                        .campusAddress(campusEntity.getCampusAddress())
                        .campusName(campusEntity.getCampusName())
                        .updateTime(LocalDateTime.now())
                        .build());

        return result > 0;
    }

    /**
     * 根据id删除校区信息
     *
     * @param id
     */
    @Override
    public void deleteCampus(Integer id) {
        campusDao.deleteCampus(id);
    }
}
